package es.mahulo.battleship.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import es.mahulo.battleship.api.dao.UserDao;
import es.mahulo.battleship.model.User;
@Repository
public class UserDaoImpl implements UserDao {
	private EntityManager entityManager;
	@PersistenceContext  
    public void setEntityManager(EntityManager entityManager)   {  
		this.entityManager = entityManager;  
    }  


	@Override
	@Transactional
	public User find(Long id) {
		return entityManager.find(User.class, id);
	}


	@Override
	public User findByName(String name) {
		
		Query q = entityManager.createQuery("SELECT u FROM User u WHERE u.name=:name",User.class);
		q.setParameter("name", name);

		User user = (User)q.getSingleResult();

		return user;
	}

}
